package com.mine.rocketmq.producer;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;

import java.util.List;

/**
 * @author weixiao
 * @date 2024/11/7 21:45
 */
public class OrderProducer {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("group_producer_test");
        producer.setNamesrvAddr("172.16.1.137:9876");
        producer.start();
        for (int i = 0; i < 100; i++) {
            Message message = new Message();
            message.setTopic("order_msg_topic");
            message.setBody(("这是第" + i + "条顺序消息").getBytes());
            producer.send(message, new MessageQueueSelector() {
                @Override
                public MessageQueue select(List<MessageQueue> list, Message message, Object args) {
                    System.out.println(list.size());
                    Integer id = (Integer) args;
                    int index = id % list.size();
                    return list.get(index);
                }
            }, i);
        }
        producer.shutdown();
    }
}
